/*
 * my_server.cpp
 * 
 * 使用 GameServerFramework 的示例服务器
 * 展示如何快速开发新服务器
 */

#include <gameserver/server/BaseServer.h>
#include <gameserver/protocol/PacketHeader.h>
#include "pbmsg/src/my_protocol.pb.h"
#include "pbmsg/src/test/test_msg.pb.h"

#include <muduo/base/Logging.h>
#include <iostream>

// 定义消息ID
enum MyMessageId : uint16_t {
    MSG_HELLO_REQUEST = 1,
    MSG_HELLO_RESPONSE = 2,
};

/**
 * @brief Hello 服务器
 * 
 * 功能：接收客户端的 HelloRequest，返回问候语
 */
class HelloServer : public framework::BaseServer {
public:
    explicit HelloServer(const framework::ServerConfig& config)
        : BaseServer(config) {}
    
protected:
    /**
     * @brief 初始化 - 注册消息处理器
     */
    bool onInit() override {
        // 注册 HelloRequest 处理器
        registerMessageHandler<protocol::HelloRequest>(
            MSG_HELLO_REQUEST,
            std::bind(&HelloServer::onHelloRequest, this,
                     std::placeholders::_1,
                     std::placeholders::_2,
                     std::placeholders::_3));
        
        LOG_INFO << "HelloServer: Message handlers registered";
        return true;
    }
    
    /**
     * @brief 启动后回调
     */
    void onStart() override {
        LOG_INFO << "========================================";
        LOG_INFO << "  HelloServer Started Successfully!";
        LOG_INFO << "  Using GameServerFramework v1.0.0";
        LOG_INFO << "========================================";
        LOG_INFO << "Ready to accept connections...";
    }
    
    /**
     * @brief 会话连接回调
     */
    void onSessionConnected(const framework::SessionPtr& session) override {
        LOG_INFO << "New client connected: " << session->sessionId();
    }
    
    /**
     * @brief 会话断开回调
     */
    void onSessionDisconnected(const framework::SessionPtr& session) override {
        LOG_INFO << "Client disconnected: " << session->sessionId();
    }

private:
    /**
     * @brief 处理 Hello 请求
     */
    void onHelloRequest(const framework::SessionPtr& session,
                       const protocol::HelloRequest& request,
                       muduo::Timestamp time) {
        LOG_INFO << "Session " << session->sessionId()
                 << " hello request from: " << request.name();
        
        // 构造响应
        protocol::HelloResponse response;
        response.set_greeting("Hello, " + request.name() + "! Welcome to GameServerFramework!");
        
        // 发送响应
        sendToClient(session->sessionId(), MSG_HELLO_RESPONSE, response);
        
        LOG_DEBUG << "Hello response sent to: " << request.name();
    }
};

int main(int argc, char* argv[]) {
    // 设置日志级别
    muduo::Logger::setLogLevel(muduo::Logger::INFO);
    
    // 加载配置文件
    std::string configFile = "config.json";
    if (argc > 1) {
        configFile = argv[1];
    }
    
    framework::ServerConfig config;
    if (!config.loadFromFile(configFile)) {
        LOG_FATAL << "Failed to load config from: " << configFile;
        return -1;
    }
    
    // 创建并运行服务器
    HelloServer server(config);
    
    LOG_INFO << "========================================";
    LOG_INFO << "  My Server - GameServerFramework Demo";
    LOG_INFO << "  Server Name: " << config.serverName;
    LOG_INFO << "  Listen: " << config.network.ip << ":" << config.network.port;
    LOG_INFO << "========================================";


    protocol::TestMessage testmsg;

    return server.run();  // 一行启动！
}

